<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <title>Express Checkout</title>
        <link href="css/bootstrap.css" rel="stylesheet" type="text/css" />
        <script src="js/bootstrap.min.js"></script>
        <script src="js/div.js"></script>
    </head>
    <body>
        <?php
        session_start();
        if (isset($_GET['token'])) {

            $token = $_GET['token'];
            $_SESSION['TOKEN'] = $token;

            $nvp = array(
                'TOKEN' => $token,
                'METHOD' => 'GetExpressCheckoutDetails',
                'VERSION' => '108.0',
                'PWD' => $_SESSION['PWD'],
                'USER' => $_SESSION['USER'],
                'SUBJECT' => $_SESSION['SUBJECT'],
                'SIGNATURE' => $_SESSION['SIGNATURE']
            );

            $curl = curl_init();

            curl_setopt($curl, CURLOPT_URL, $_SESSION['ENDPOINT']);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($nvp));

            $response = urldecode(curl_exec($curl));

            $responseNvp = array();

            curl_close($curl);

            if (preg_match_all('/(?<name>[^\=]+)\=(?<value>[^&]+)&?/', $response, $matches)) {
                foreach ($matches['name'] as $offset => $name) {
                    $responseNvp[$name] = $matches['value'][$offset];
                }
            }

            echo "</br>";
            echo "GetExpressCheckout";
            echo "</p>";
            echo "Request";
            echo "<pre>";
            print_r($nvp);
            echo"</pre>";
            echo '<p>Response</p>';
            echo '<pre>';
            print_r($responseNvp);
            echo '</pre>';

            if (isset($responseNvp['TOKEN']) && isset($responseNvp['ACK'])) {
                if ($responseNvp['TOKEN'] == $token && $responseNvp['ACK'] == 'Success') {

                    $nvp = null;
                    $nvp = array(
                        'TOKEN' => $token,
                        'METHOD' => 'DoExpressCheckoutPayment',
                        'VERSION' => $responseNvp['VERSION'],
                        'PWD' => $_SESSION['PWD'],
                        'USER' => $_SESSION['USER'],
                        'SIGNATURE' => $_SESSION['SIGNATURE'],
                        'PAYERID' => $responseNvp['PAYERID'],
                        'AMT' => '300.00',
                        'PAYMENTACTION' => $_SESSION['PAYMENTREQUEST_0_PAYMENTACTION'],
                        'SUBJECT' => $_SESSION['SUBJECT'],
                        'CURRENCYCODE' => $responseNvp['CURRENCYCODE']
                    );

                    $curl = curl_init();

                    curl_setopt($curl, CURLOPT_URL, $_SESSION['ENDPOINT']);
                    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt($curl, CURLOPT_POST, 1);
                    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($nvp));

                    $response = urldecode(curl_exec($curl));

                    $responseNvp = null;

                    curl_close($curl);

                    if (preg_match_all('/(?<name>[^\=]+)\=(?<value>[^&]+)&?/', $response, $matches)) {
                        foreach ($matches['name'] as $offset => $name) {
                            $responseNvp[$name] = $matches['value'][$offset];
                        }
                    }

                    echo "</br>";
                    echo "DoExpressCheckout";
                    echo "Request";
                    echo "<pre>";
                    print_r($nvp);
                    echo"</pre>";
                    echo '<p>Response</p>';
                    echo '<pre>';
                    print_r($responseNvp);
                    echo '</pre>';
                    ?>

                    <form action="getExpressCheckoutDetails.php" method="post">
                        </br>
                        <p>Executar novamente GetExpressCheckoutDetails</p>
                        <input type="submit" value="GetExpressCheckoutDetails" />
                    </form>

                    <?php
                } else {
                    echo "</br>";
                    echo "Request";
                    echo "<pre>";
                    print_r($nvp);
                    echo"</pre>";
                    echo '<p>Response</p>';
                    echo '<pre>';
                    print_r($responseNvp);
                    echo '</pre>';
                }
            } else {
                echo "</br>";
                echo "Request";
                echo "<pre>";
                print_r($nvp);
                echo"</pre>";
                echo '<p>Response</p>';
                echo '<pre>';
                print_r($responseNvp);
                echo '</pre>';
            }
        } else {
            echo "</br>";
            echo "Request";
            echo "<pre>";
            print_r($nvp);
            echo"</pre>";
            echo '<p>Response</p>';
            echo '<pre>';
            print_r($responseNvp);
            echo '</pre>';
        }
        ?>
    </body>
</html>





